﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using THSERP.BaseProvider;
using THSLib;
using THSERP.BusinessEntities;
namespace THSERP.Modules.TaxDec
{
    public partial class guiSelectInvoice :THSERPScreen
    {
        public guiSelectInvoice()
        {
            InitializeComponent();
            
        }

        private void guiSelectInvoice_Load(object sender, EventArgs e)
        {
            fld_dgcARInvoices.Screen = this;
            fld_dgcARInvoices.InitializeControl();
            fld_dteSearchFromARInvoiceDate.Screen = this;
            fld_dteSearchToARInvoiceDate.Screen = this;
            fld_dteSearchFromARInvoiceDate.DateTime = THSApp.WorkingDate;
            fld_dteSearchToARInvoiceDate.DateTime = THSApp.WorkingDate;
            //
            fld_lkeGEVATs.Screen = this;
            fld_lkeGEVATs.InitializeControl();
            TaxDecEntities entity = (TaxDecEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            entity.lstARInvoices.Invalidate(new DataSet());
            DataSet ds = new GEVATsController().GetAllObjects();
            fld_lkeFK_GEVATID.Properties.DataSource = ds.Tables[0];
           // fld_lkeGEVATs.Properties.DataSource = ds.Tables[0];

        }

        private void fld_btnSearch_Click(object sender, EventArgs e)
        {
            String strQuery = String.Format("SELECT * from ARInvoices WHERE (CONVERT(VARCHAR(10), [ARInvoiceDate], 112) >= CONVERT(VARCHAR(10),'{0}', 112)) AND (CONVERT(VARCHAR(10), [ARInvoiceDate], 112) <= CONVERT(VARCHAR(10),'{1}', 112)) AND ((FK_GEVATID=0)OR (FK_GEVATID={2}))  AND AAIsPrinted=0 AND AAStatus='Alive'", fld_dteSearchFromARInvoiceDate.DateTime.ToString("yyyyMMdd"), fld_dteSearchToARInvoiceDate.DateTime.ToString("yyyyMMdd"),Convert.ToInt32(fld_lkeGEVATs.EditValue));
            DataSet ds = new ARInvoicesController().GetDataSet(strQuery);
            TaxDecEntities entity = (TaxDecEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            entity.lstARInvoices.Invalidate(ds);
            fld_dgcARInvoices.RefreshDataSource();
            //fld_dgvARInvoices.RefreshData();
            fld_chkSelectAllInvoice.Checked = false;
        }

        private void fld_btnOK_Click(object sender, EventArgs e)
        {
             TaxDecEntities entity = (TaxDecEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            if (fld_lkeFK_GEVATID.EditValue == null)
            {
                MessageBox.Show("Chưa chọn thuế!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (!entity.ARInvoicesList.Exists("AASelected", true))
            {
                MessageBox.Show("Chưa chọn hóa đơn khai báo thuế!","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void fld_chkSelectAllInvoice_CheckStateChanged(object sender, EventArgs e)
        {
            TaxDecEntities entity = (TaxDecEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            if (Convert.ToBoolean(fld_chkSelectAllInvoice.EditValue))
            {

                for (int i = 0; i < entity.ARInvoicesList.Count; i++)
                {
                     ((ARInvoicesInfo)entity.ARInvoicesList[i]).AASelected=true;
                    //objARInvoicesInfo.AASelected = true;
                    //lst.Add(objARInvoicesInfo);
                }
            }
            else
            {
                for (int i = 0; i < entity.ARInvoicesList.Count; i++)
                {
                    ((ARInvoicesInfo)entity.ARInvoicesList[i]).AASelected = false;
                    //ARInvoicesInfo objARInvoicesInfo = ((ARInvoicesInfo)entity.ARInvoicesList[i]);
                    //objARInvoicesInfo.AASelected = false;
                    //lst.Add(objARInvoicesInfo);
                }
            }
            fld_dgvARInvoices.RefreshData();
            fld_dgcARInvoices.Views[0].RefreshData();
        }

        
    }
}